<?php
declare (strict_types=1);

namespace app\course\model\collection\admin;

use app\common\model\BaseModel;
use app\user\model\user\admin\Account;
use think\model\relation\BelongsTo;

/**
 * Class Collection
 *
 * @package app\course\model\collection\admin
 */
class Collection extends BaseModel
{

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'user_collection';

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 全局查询范围
     *
     * @var array
     */
    protected $globalScope = ['sourceType'];

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'          => 'integer',
        'source_type' => 'string',
        'source_id'   => 'integer',
    ];

    /**
     * 资源类型
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/27
     *
     * @param        $query
     */
    public function scopeSourceType($query)
    {
        $query->where('source_type', 'IN', 'video,live,offline,classes');
    }

    /**
     * 关联用户
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-30
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'user_id')->with(['avatar_url'])->removeOption('soft_delete')->removeWhereField('mhm_id');
    }

}